package com.swgk.core.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.swgk.core.R;
import com.swgk.core.util.MToast;

public class InputDialog extends Dialog {
    private Context context;
    private TextView titleView;
    private TextView confirmView;
    private TextView cancelView;
    private TextView contentView;
    private boolean inputNull = false;
    private DialogCallBack dialogCallBack;

    public InputDialog(Context context) {
        super(context, R.style.DialogInputStyle);
        this.context = context;
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
    }

    public InputDialog(Context context, int style) {
        super(context, style);
        this.context = context;
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
    }

    public InputDialog builder() {
        View view = LayoutInflater.from(context).inflate(
                R.layout.dialog_input, null);
        setContentView(view);
        titleView = view.findViewById(R.id.tv_title);
        confirmView = view.findViewById(R.id.tv_logout_confirm);
        cancelView = view.findViewById(R.id.tv_logout_cancel);
        contentView = view.findViewById(R.id.et_content);
        cancelView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                if (dialogCallBack != null) {
                    dialogCallBack.onCancel();
                }
            }
        });
        confirmView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = contentView.getText().toString();
                if (!inputNull) {
                    if (content.equals("")) {
                        MToast.showToast(context, "输入内容不能为空");
                        return;
                    }
                }
                if (dialogCallBack != null) {
                    dialogCallBack.onResult(content);
                }
                dismiss();
            }
        });
        return this;
    }

    public InputDialog setInputNull(boolean inputNull) {
        this.inputNull = inputNull;
        return this;
    }

    public InputDialog setTitle(String title) {
        if ("".equals(title)) {
            titleView.setText("标题");
        } else {
            titleView.setText(title);
        }
        return this;
    }

    public InputDialog setHint(String hint) {
        if ("".equals(hint)) {
            contentView.setHint("请输入内容");
        } else {
            contentView.setHint(hint);
        }
        return this;
    }

    public InputDialog setContent(String content) {
        if ("".equals(content)) {
            contentView.setText("");
        } else {
            contentView.setText(content);
        }
        return this;
    }

    public InputDialog setCancel(String canel) {
        if ("".equals(canel)) {
            cancelView.setText("取消");
        } else {
            cancelView.setText(canel);
        }
        return this;
    }

    public InputDialog setConfirm(String confirm) {
        if ("".equals(confirm)) {
            confirmView.setText("确定");
        } else {
            confirmView.setText(confirm);
        }
        return this;
    }

    public InputDialog setDialogCallBack(DialogCallBack dialogCallBack) {
        this.dialogCallBack = dialogCallBack;
        return this;
    }

    public interface DialogCallBack {
        void onResult(String content);
        void onCancel();
    }
}
